package org.jeecg.modules.rental.service.impl;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.jeecg.modules.rental.entity.RentalEBikeType;
import org.jeecg.modules.rental.entity.RentalPackage;
import org.jeecg.modules.rental.entity.RentalPackageConfig;
import org.jeecg.modules.rental.enums.PackagesTypeEnums;
import org.jeecg.modules.rental.mapper.RentalEBikeTypeMapper;
import org.jeecg.modules.rental.mapper.RentalPackageConfigMapper;
import org.jeecg.modules.rental.mapper.RentalPackageMapper;
import org.jeecg.modules.rental.service.IRentalPackageConfigService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Description: rental_package_config_new
 * @Author: jeecg-boot
 * @Date:   2024-08-02
 * @Version: V1.0
 */
@Service
@RequiredArgsConstructor
public class RentalPackageConfigServiceImpl extends ServiceImpl<RentalPackageConfigMapper, RentalPackageConfig> implements IRentalPackageConfigService {

    private final RentalEBikeTypeMapper rentalCarTypeMapper;

    private final RentalPackageMapper rentalPackageMapper;

    @Override
    public IPage<RentalPackageConfig> selectJoinPage(Page page, Wrapper wrapper) {
        IPage<RentalPackageConfig> pageList = baseMapper.selectJoinPage(page, wrapper);
        if(ObjectUtil.isNotEmpty(pageList) && ObjectUtil.isNotEmpty(pageList.getRecords())){
            List<Long> carTypeIdList = pageList.getRecords()
                    .stream().map(RentalPackageConfig::getCarTypeId).collect(Collectors.toList());
            List<RentalEBikeType> carTypeList = rentalCarTypeMapper.selectBatchIds(carTypeIdList);

            pageList.getRecords().stream().forEach(item->{
                Optional<RentalEBikeType>  typeOptional = carTypeList.stream().filter(a-> a.getId().equals(item.getCarTypeId())).findFirst();
                typeOptional.ifPresent(a-> item.setCarTypeName(a.getCarTypeName()));
                if(StrUtil.isNotBlank(item.getPackageType())){
                    item.setPackageTypeName(PackagesTypeEnums.getEnumByValue(item.getPackageType()).getLabel());
                }
            });
        }
        return pageList;
    }

    @Override
    public List<RentalPackageConfig> rentalConfigNews(Long carTypeId) {
        RentalPackage rentalPackage = rentalPackageMapper
                .selectOne(new QueryWrapper<RentalPackage>().eq("status", "1"));
        List<RentalPackageConfig> list = baseMapper.selectList(new QueryWrapper<RentalPackageConfig>()
                .eq("package_id",rentalPackage.getId())
                .eq("car_type_id",carTypeId)
                .eq("is_rental", "1"));

        if(ObjectUtil.isNotEmpty(list)){
            list.stream().forEach(item->{
                item.setPackageUtil(PackagesTypeEnums.getEnumByValue(item.getPackageType()).getKey());
            });
        }
        return list;
    }

    @Override
    public List<RentalPackageConfig> renewConfigNews(Long carTypeId) {
        RentalPackage rentalPackage = rentalPackageMapper
                .selectOne(new QueryWrapper<RentalPackage>().eq("status", "1"));
        List<RentalPackageConfig> list = baseMapper.selectList(new QueryWrapper<RentalPackageConfig>()
                .eq("package_id",rentalPackage.getId())
                .eq("car_type_id",carTypeId)
                .eq("is_renew", "1"));
        if(ObjectUtil.isNotEmpty(list)){
            list.stream().forEach(item->{
                item.setPackageUtil(PackagesTypeEnums.getEnumByValue(item.getPackageType()).getKey());
            });
        }
        return list;
    }

    @Override
    public LocalDateTime calcPkgEndTime(Long packageConfId, LocalDateTime startTime) {
        RentalPackageConfig packageConfigNew = baseMapper.selectById(packageConfId);
        PackagesTypeEnums packageType = PackagesTypeEnums.getEnumByValue(packageConfigNew.getPackageType());
        LocalDateTime endTime;
        switch (packageType) {
            case MINUTE:
                endTime = startTime.plusMinutes((long) packageConfigNew.getDuration().doubleValue());
                break;
            case HOURS:
                endTime = startTime.plusHours((long) packageConfigNew.getDuration().doubleValue());
                break;
            case DAILY:
                endTime = startTime.plusDays((long) packageConfigNew.getDuration().doubleValue());
                break;
            case MONTH:
                endTime = startTime.plusMonths((long) packageConfigNew.getDuration().doubleValue());
                break;
            default:
                throw new IllegalArgumentException("Invalid package type: " + packageConfigNew.getPackageType());
        }
        return endTime;
    }

}
